package timerDemo;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class MyThreadPoolExecutor {
    //创建一个阻塞队列
    BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();

    public MyThreadPoolExecutor(int n) {
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(() -> {

                try {
                    while (true) {
                        Runnable task = queue.take();
                        task.run();
                    }

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            });
            thread.start();
        }
    }
    //创建方法提交任务
    public void sumbit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

}

public class Demo1 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoolExecutor executor = new MyThreadPoolExecutor(5);
        for (int i = 0; i < 5; i++) {
            executor.sumbit(()->{
                System.out.println(Thread.currentThread().getName());
            });
        }
    }
}
